import { HTTP } from '../../../utils/http.js'
import api from '../../../utils/api.js'
import { config } from '../../../utils/config'
import utils from "../../../utils/util"

let http = new HTTP()
var app = getApp();


Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		imgUrl: config.imgUrl,
		payBtn: true,
		maskShow: false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let order_no = options.order;
		// let order_no = 'IM20200801801630788838406453';
		this.setData({
			order_no
		})
		this.getOrderDetail();
	},
	preventD() {
		return
	},
	// 获取订单详情
	getOrderDetail() {
		let that = this;

		let order_no = this.data.order_no;
		http.request({
			url: api.inteGetRefundDetails,
			method: 'GET',
			data: {
				service_no: order_no
			},
			success: (res) => {
				wx.hideLoading();
				let result = res.data
				if (res.code == 200) {
					that.setData(result);
					that.setData({
						create_time: utils.timestampToTime(result.create_time)
					})
					that.inteGetReasonList();
				}
			},
			fail: (res) => {
			}
		})
	},

	// 申请原因 
	inteGetReasonList() {
		let that = this
		utils.getInfo(api.inteGetReasonList, "GET", {}).then(res => {
			let reasonArr = [];
			for (const key in res) {
				reasonArr.push({ id: key, reason: res[key] });
			}
			let reasonList = reasonArr;
			reasonList.forEach(item => {
				if (that.data.reason == item.id) {
					that.setData({
						refundReason: item.reason,
					})
				}
			})
		})
	},
	expressNameHand(e) {
		let expressName = e.detail.value;
		this.setData({
			expressName
		})
	},
	expressNumHand(e) {
		let expressNumber = e.detail.value;
		this.setData({
			expressNumber
		})
	},
	expressBtn() {
		let that = this
		let expressName = this.data.expressName;
		let expressNumber = this.data.expressNumber;
		if (!expressName || !expressNumber) {
			wx.showToast({
				title: '请填写完整的快递信息',
				icon: 'none',
				duration: 1500
			})
			return;
		} else {
			// 用户寄回商品
			let data = {
				service_no: this.data.service_no,
				express_name: expressName,
				express_no: expressNumber
			}

			http.request({
				url: api.inteSendBack,
				method: "POST",
				data: data,
				success: (res) => {
					console.log(res);
					if (res.code == 200) {
						wx.showToast({
							title: '快递信息提交成功',
							icon: 'success',
							duration: 1500
						})
						that.setData({
							maskShow: false
						})
						// that.getOrderDetail();
						that.onLoad({ order: that.data.service_no });
					} else {

						wx.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1500
						});

						setTimeout(() => {
							this.setData({
								maskShow: false
							})
						}, 1500);

						return;
					}
				},
				fail: (res) => {

				}
			})



		}
	},
	copyBtn() {
		var that = this;
		let back_address = that.data.back_address;
		wx.setClipboardData({
			//准备复制的数据
			data: back_address.name + back_address.phone + back_address.address,
			success: function (res) {
				wx.showToast({
					title: '复制成功',
					icon: 'none'
				});
			}
		});
	},
	//删除订单
	deleteOrder() {
		let that = this;
		let order_no = that.data.order_no;
		const pages = getCurrentPages()
		const prevPage = pages[pages.length - 2] // 上一页// 调用上一个页面的setData 方法，将数据存储

		wx.showModal({
			title: '提示',
			content: '确定删除订单',
			confirmColor: '#EA5D56',
			success(res) {
				if (res.confirm) {
					prevPage.setData({
						deleteOrder: order_no
					})
					wx.navigateBack({
						delta: 1
					})
				} else if (res.cancel) {
					return;
				}
			}
		})

	},

	// 取消订单
	cancelOrder() {
		let that = this;
		let order_no = that.data.order_no;
		wx.showModal({
			title: '提示',
			content: '确定取消订单',
			confirmColor: '#EA5D56',
			success(res) {
				if (res.confirm) {

					http.request({
						url: api.integralCancelOrder,
						method: "POST",
						data: {
							order_no
						},
						success: (res) => {
							console.log(res);
							if (res.code == 200) {
								that.getOrderDetail();
								// 给上一页传递数据
								const pages = getCurrentPages()
								const prevPage = pages[pages.length - 2]
								prevPage.setData({
									cancelOrderBack: order_no
								})

							}
						},
						fail: (res) => {

						}
					})

				} else if (res.cancel) {
					return;
				}
			}
		})

	},

	// 取消订单
	cancelOrderTime() {
		let that = this;
		let order_no = that.data.order_no;

		http.request({
			url: api.integralCancelOrder,
			method: "POST",
			data: {
				order_no
			},
			success: (res) => {
				console.log(res);
				if (res.code == 200) {
					clearInterval(timer);
					that.getOrderDetail();
					// 给上一页传递数据
					const pages = getCurrentPages()
					const prevPage = pages[pages.length - 2]
					prevPage.setData({
						cancelOrderBack: order_no
					})
				}
			},
			fail: (res) => {

			}
		})


	},
	// 电话
	service(e) {
		let phone = e.currentTarget.dataset.phone;
		console.log(phone);
		wx.makePhoneCall({
			phoneNumber: phone.toString()
		})
	},

	onReady: function () {

	},


	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		this.getOrderDetail();
	},

	closeMask() {
		this.setData({
			maskShow: false
		})
	},

	openMask() {
		this.setData({
			maskShow: true
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},



	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})